package groupTest;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

// 实现数据报的接受线程
public class TestRecv extends Thread {
	MulticastSocket s = null;

	public TestRecv(MulticastSocket s) {
		this.s = s;
	};

	public void run() {
		byte[] buf = new byte[1024];
		DatagramPacket recv = new DatagramPacket(buf, buf.length);
		try {
			while (true) {
				s.receive(recv);
				String str = new String(recv.getData(), 0, recv.getLength());
				System.out.println(str);
			}
		} catch (IOException e1) {
			System.out.println("接受失败");
		}
	}

	public static void main(String[] args) throws Exception {
		MulticastSocket s = new MulticastSocket(6789);

		InetAddress group = InetAddress.getByName("238.9.6.132");
		s.joinGroup(group);
		System.out.println(group);
		System.out.println("Over");

		new TestRecv(s).start();
	}
}